var CONSTS = require('../util/consts');
var moment = require('moment');
var async = require('async');
var Common = require('../util/common');
var CONFIG = require('../config/config').Config;
// var nodemailer = require('nodemailer');
var thenjs = require('thenjs');
var crypto = require("crypto");
var validator = require('validator');
var models = require('../models');
var fs = require('fs-extra');
var _ = require('lodash');
var user = require('./user');
var global = require('../config/genset').Global;
exports.index = function(req, res, next) {
    models.Tag.find({})
        .limit(15)
        .exec(function(err, tags) {
            if (err) {
                next();
                return;
            } else if (tags.lenght == 0) {
                res.render('index', {
                    tags: [],
                    global: global
                });
            } else {
                res.render('index', {
                    tags: tags,
                    global: global
                });
            }
        });
};

exports.singleshow = function(req, res, next) {
    var articleId = req.params.id;

    models.Article.findOne({
            _id: articleId
        })
        .populate('author', '-name -pwd -sess')
        .exec(function(err, article) {
            if (err || !article) {
                next();
                return;
            }

            res.render('article', {
                article: article,
                global: global
            });
        });
}
exports.tagArtshow = function(req, res, next) {
    var tag = req.params.tag;

    models.Tag.find({
            name: tag
        })
        .populate('author article')
        .exec(function(err, tags) {
            if (err) {
                next();
                return;
            } else if (tags.lenght == 0) {
                res.render('tagsart', {
                    tags: [],
                    global: global
                });
            } else {
                res.render('tagsart', {
                    tags: tags,
                    global: global
                });
            }
        });
}
exports.authorArt = function(req, res, next) {
    var aid = req.params.id;

    models.User.findOne({
        _id: aid
    }, function(err, user) {
        if (err || !user) {
            next();
            return;
        } else {
            res.render('authorart', {
                id: aid,
                user: user,
                global: global
            });
        }
    });

}
exports.admin = function(req, res, next) {
    res.render('admin/index', {
        global: global
    });
};

exports.adminLogout = function(req, res, next) {
    user.clearSess(req.u, res, function(err) {
        if (err) {
            next(err);
            return;
        }
        res.redirect('admin');
    });
};

exports.adminSetting = function(req, res, next) {
    res.render('admin/setting', {
        global: global
    });
};
exports.artcreate = function(req, res, next) {
    res.render('admin/artcreate', {
        global: global
    });
}
exports.artlist = function(req, res, next) {
    models.Article.find({

        })
        .populate('author')
        .sort({
            created: -1
        })
        .exec(function(err, articles) {
            if (err) {
                next();
                return;
            }
            if (articles.length == 0) {
                res.render('admin/artlist', {
                    articles: [],
                    global: global
                });
                return;
            }

            res.render('admin/artlist', {
                articles: articles,
                global: global
            });
        });
}
exports.artupdata = function(req, res, next) {
    res.render('admin/artupdata', {
        global: global
    });
}
exports.userlist = function(req, res, next) {
    models.User.find({}, function(err, users) {
        if (err) {
            next();
            return;
        }
        if (users.length == 0) {
            res.render('admin/userList', {
                users: [],
                global: global
            });
        } else {

            res.render('admin/userList', {
                users: users,
                global: global
            });
        }
    });
}
exports.userdata = function(req, res, next) {
    var rid = req.params.id;
    var u = req.u;
    var template = '';

    if (rid == u._id) {
        template = 'admin/userInfo';
    } else {
        template = 'admin/authorInfo';
    }

    models.User.findOne({
        _id: rid
    }, function(err, user) {
        if (err || !user) {
            next();
            return;
        } else {
            res.render(template, {
                user: user,
                global: global
            });
        }
    });
}
exports.genset = function(req, res, next) {
    res.render("admin/genset", {
        global: global
    });

}